<template>
    <el-form :model="form"
             :rules="rules"
             ref="formRef">
        <el-form-item label="姓名"
                      prop="name"
                      :label-width="formLabelWidth">
            <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="电话"
                      prop="phone"
                      :label-width="formLabelWidth">
            <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="代理等级"
                      prop="delegeteLevel"
                      :label-width="formLabelWidth">
            <el-select v-model="form.levelId"
                       placeholder="请选择代理等级">
                <el-option v-for="(delegete,index) in delegeteOptions"
                           :key="index"
                           :label="delegete.name"
                           :value="delegete.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item :label-width="formLabelWidth">
            <el-button plain
                       @click="resetForm('formRef')">取消</el-button>
            <el-button type="primary"
                       @click="submitForm('formRef')">确定</el-button>
        </el-form-item>
    </el-form>
</template>

<script>
export default {
    props: ['customerId'],
    data() {
        return {
            form: {
                customerId: '', //客户id
                name: '',  //姓名
                phone: '', //电话
                levelId: '', //代理等级
            },
            delegeteOptions: [],
            formLabelWidth: '100px',
            rules: {
                name: [
                    { required: true, message: '请输入姓名', trigger: 'blur' },
                    { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '请输入电话', trigger: 'blur' },
                ],
                levelId: [
                    { required: true, message: '请选择等级', trigger: 'blur' },
                ],
            },
        }
    },
    mounted() {
        this.form.customerId = this.customerId
        this.$service.delegeteList().then(res => {

            this.delegeteOptions = res.data;
        });
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    this.$service.bindDelegete(this.form)
                        .then(res => {
                            this.$emit('submit', false)
                            if (res.code == '00000') {
                                this.$message({
                                    message: '设置代理成功'
                                })
                                this.$emit('submit', false)
                                this.$refs[formName].resetFields();
                            } else {
                                this.$message({
                                    message: res.message ? res.message : '出错了'
                                })
                            }

                        })
                }
            });
        },
        resetForm(formName) {
            this.$emit('submit', false)
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style>
</style>